package com.wanmait.hotelManage.servlet;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.wanmait.hotelManage.dao.NewsDAO;
import com.wanmait.hotelManage.vo.News;
import com.wanmait.hotelManage.vo.Pager;

/**
 * Servlet implementation class NewsServlet
 */
@WebServlet("/manage/NewsServlet")
@MultipartConfig
public class NewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if("init".equals(action)) {
			this.init(request, response);
		}else if("de".equals(action)) {
			this.de(request, response);
		}else if("del".equals(action)) {
			this.del(request, response);
		}else if("insert".equals(action)) {
			this.insert(request, response);
		}else if("initTwo".equals(action)) {
			this.initTwo(request, response);
		}else if("search".equals(action)) {
			this.search(request, response);
		}else if("searchTwo".equals(action)) {
			this.searchTwo(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}
	/**
	 * 搜索分页
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void searchTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer pageNo=Integer.parseInt(request.getParameter("id"));
		String text=request.getParameter("text");
		Pager pager=new Pager();
		pager.setPageNo(pageNo);
		pager.setPageSize(4);
		Integer rows=NewsDAO.getNewsDAO().getrowss(text);
		System.out.println(rows);
		pager.setPageCount(rows);
		List<News>news=NewsDAO.getNewsDAO().findtitle(text,pager);
		request.setAttribute("news", news);
		request.setAttribute("pager", pager);
		request.setAttribute("text", text);
		request.getRequestDispatcher("/manage/news/searchTwo.jsp").forward(request, response);
	}
	/**
	 * 搜索
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String text=request.getParameter("text");
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(4);
		Integer rows=NewsDAO.getNewsDAO().getrowss(text);
		System.out.println(rows+"111111");
		pager.setPageCount(rows);
		List<News>news=NewsDAO.getNewsDAO().findtitle(text,pager);
		request.setAttribute("news", news);
		request.setAttribute("pager", pager);
		request.setAttribute("text", text);
		request.getRequestDispatcher("/manage/news/search.jsp").forward(request, response);
	}
	/**
	 * 页码变换的页面
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void initTwo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Integer no=Integer.parseInt(request.getParameter("id"));
		Pager pager=new Pager();
			pager.setPageNo(no);
			pager.setPageSize(4);
			int rows=NewsDAO.getNewsDAO().getrows();
			pager.setPageCount(rows);
			List<News>news=NewsDAO.getNewsDAO().findall(pager);
			request.setAttribute("news", news);
			request.setAttribute("pager", pager);
			request.getRequestDispatcher("/manage/news/initTwo.jsp").forward(request, response);
	}
	/**
	 * 添加
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void insert(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Part part=request.getPart("jpg");
		String head=part.getHeader("content-disposition");
		int i=head.lastIndexOf(".");
		String f=head.substring(i,head.length()-1);
		String filepath=request.getServletContext().getRealPath("/static/manage/images/news");
		String fileName=UUID.randomUUID().toString();
		part.write(filepath+"/"+fileName+f);
		String par=fileName+f;
		String content=request.getParameter("content");
		String title=request.getParameter("title");
		String intr=request.getParameter("intr");
		News news=new News();
		news.setPic(par);
		news.setContent(content);
		news.setTitle(title);
		news.setIntr(intr);
		NewsDAO.getNewsDAO().insert(news);
		response.sendRedirect(request.getContextPath()+"/manage/NewsServlet?action=init");
	}
	/**
	 * ajax的批量删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		NewsDAO.getNewsDAO().del(id);
	}
//	/**
//	 * ajax的批量通过
//	 * @param request
//	 * @param response
//	 * @throws ServletException
//	 * @throws IOException
//	 */
//	protected void bys(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String id=request.getParameter("id");
//		System.out.println(id+111);
//		NewsDAO.getNewsDAO().bys(id);
//	}
//	
//	/**
//	 * 通过
//	 * @param request
//	 * @param response
//	 * @throws ServletException
//	 * @throws IOException
//	 */
//	protected void by(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		String id=request.getParameter("id");
//		NewsDAO.getNewsDAO().by(Integer.parseInt(id));
//		response.sendRedirect(request.getContextPath()+"/manage/NewsServlet?action=init");
//	}
	/**
	 * ajax的删除
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void de(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id=request.getParameter("id");
		NewsDAO.getNewsDAO().de(Integer.parseInt(id));
	}
	/**
	 * 初始化
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void init(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Pager pager=new Pager();
		pager.setPageNo(1);
		pager.setPageSize(4);
		Integer rows=NewsDAO.getNewsDAO().getrows();
		System.out.println(rows);
		pager.setPageCount(rows);
		List<News>news=NewsDAO.getNewsDAO().findall(pager);
		request.setAttribute("news", news);
		request.setAttribute("pager", pager);
		request.getRequestDispatcher("/manage/news/list.jsp").forward(request, response);
	}
}
